/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.model.Users;
import br.com.foxline.service.MessageService;
import br.com.foxline.service.UserService;
import java.io.IOException;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author luciano
 */
@ManagedBean
@SessionScoped
public class ManagerCurrentUser implements Serializable {

    @EJB
    UserService userService;
    @EJB
    MessageService messageService;
    boolean hasNews = false;
    int cout;
    Users users;

    @PostConstruct
    public void init() {
        users = userService.userByLoginName(userService.getCurrentUser());
        count();
    }

    public int count() {
        int i = messageService.countMessagesUnread();
        hasNews = i > 0;
        return i;
    }

    public void logout() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        String contextName = servletContext.getContextPath();
        facesContext.responseComplete();
        facesContext.getExternalContext().invalidateSession();
        facesContext.getExternalContext().redirect("/CorretorWeb-war/login.jsp");
    }

    public boolean isHasNews() {
        return hasNews;
    }

    public void setHasNews(boolean hasNews) {
        this.hasNews = hasNews;
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }
}
